package com.pda.xinmazhi.ui.empty;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.pda.xinmazhi.R;
import com.pda.xinmazhi.bean.ContainerDetailBean;
import com.pda.xinmazhi.bean.ScanBean;
import com.pda.xinmazhi.bean.ScanResultBean;
import com.pda.xinmazhi.bean.request.EmptyRequest;
import com.pda.xinmazhi.bean.request.QRcodeRequest;
import com.pda.xinmazhi.contract.EmptyContract;
import com.pda.xinmazhi.dialog.AffirmDialog;
import com.pda.xinmazhi.net.NetUtils;
import com.pda.xinmazhi.presenter.EmptyPresenter;
import com.pda.xinmazhi.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.base.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
@Route(path = AppActivityKey.EMPTYOUTACTIVITY)
public class EmptyOutActivity extends BaseMvpActivity<EmptyPresenter> implements EmptyContract.View {
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_storageBin)
    TextView tvStorageBin;
    @BindView(R.id.tv_container)
    TextView tvContainer;
    @BindView(R.id.tv_articleId)
    TextView tvArticleId;
    @BindView(R.id.tv_grade)
    TextView tvGrade;
    @BindView(R.id.tv_batch)
    TextView tvBatch;
    @BindView(R.id.tv_clear)
    TextView tvClear;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.tv_move_out)
    TextView tvMoveOut;

    @Override
    public EmptyPresenter getPresenter() {
        return new EmptyPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_empty_out;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initLinsenterner() {
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                QRcodeRequest request = new QRcodeRequest();
                request.setQRCode(scanBean.getCode());
                getCode(request);
            }
        });
    }
    public void getCode(QRcodeRequest request) {
        NetUtils.geApi().scanTheQRCodeToVerify(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseAppEntity<ScanResultBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ScanResultBean> stringBaseAppEntity) throws Exception {
                        if (!stringBaseAppEntity.getCode().equals("200")) {
                            ToastUtils.showToast(mContext, stringBaseAppEntity.getMessage());
                            return;
                        }
                        if (stringBaseAppEntity.getData().getType().equals("KW")){
                            tvStorageBin.setText(stringBaseAppEntity.getData().getCode());
                            //SharedPreferencesUtil.getInstance().putString(StringKey.STORAGEBIN,stringBaseAppEntity.getData().getCode());
                        }else if (stringBaseAppEntity.getData().getType().equals("ZT")) {
                            tvContainer.setText(stringBaseAppEntity.getData().getCode());
                            presenter.getContainerDetail(stringBaseAppEntity.getData().getCode());
                        } else {
                            ToastUtils.showToast(mContext, "请扫正确的条形码");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.showToast(mContext, throwable.getMessage());
                    }
                });
    }

    @Override
    public void getData() {

    }

    @Override
    public void setData(ContainerDetailBean detailBean) {
        tvArticleId.setText(detailBean.getArticleId());
        tvBatch.setText(detailBean.getBatch());
        tvGrade.setText(detailBean.getGrade());
    }

    @Override
    public void onSuccess() {
        tvArticleId.setText("");
        tvBatch.setText("");
        tvGrade.setText("");
        tvContainer.setText("");
        tvStorageBin.setText("");
    }


    @OnClick({R.id.iv_close, R.id.tv_clear, R.id.tv_move_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finishActivity();
                break;
            case R.id.tv_clear:
                if (TextUtils.isEmpty(tvContainer.getText())){
                    showMessage("请先扫桶码！");
                    return;
                }
                AffirmDialog dialog=new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                    @Override
                    public void onConfrim() {
                        presenter.getContainerWash(tvContainer.getText().toString());
                    }
                });
                dialog.show("确定要清理吗？");
                break;
            case R.id.tv_move_out:
                if (TextUtils.isEmpty(tvContainer.getText())){
                    showMessage("请先扫桶码！");
                    return;
                }
                if (TextUtils.isEmpty(tvStorageBin.getText())){
                    showMessage("请先扫库位！");
                    return;
                }
                EmptyRequest emptyRequest=new EmptyRequest();
                emptyRequest.setContainer(tvContainer.getText().toString());
                emptyRequest.setStorageBinId(tvStorageBin.getText().toString());
                presenter.emptyOut(emptyRequest);
                break;
        }
    }
    @Override
    public void onClearSuccess() {
        tvBatch.setText("");
    }
}
